// search页的store
//search组件下的vuex封装
import { reqProductList } from "@/api";

//state
const state = {
  productList:{}, //搜索页的商品信息
}

//mutations
const mutations = {
  receive_product_list(state,productList){
    state.productList = productList;
  }
}

//actions
const actions = {
  //action之获取【搜索页  ---- 商品信息 】
  async reqProductList({commit},searchParams){

    //搜索优化之delete掉相关的空数据/数组
    searchParams = {...searchParams}
    Object.keys(searchParams).forEach((key)=>{
        if(searchParams[key] === '' || (searchParams[key] instanceof Array && searchParams[key].length) === 0 ){
          delete searchParams[key]
        }
    })

    const result = await reqProductList(searchParams)
    if(result.code === 200){
      commit('receive_product_list',result.data)
    }
  },
}
//getters
const getters = {
  //商品信息
  goodsList(){
    return state.productList.goodsList || []
  },
  //品牌下分类信息
  attrsList(){
    return state.productList.attrsList || []
  },
  //品牌信息
  trademarkList(){
    return state.productList.trademarkList || []
  }
}

export default{
  state,
  mutations,
  actions,
  getters
}